<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GerarPagamento
 *
 * @author Albuquerque
 */
require_once "../negocio/Cliente.php";
require_once "../negocio/Inscricao.php";
require_once "../PagSeguroLibrary/PagSeguroLibrary.php";

    $gerarPagamento= new GerarPagamento();
    $gerarPagamento->getParametros();
    $gerarPagamento->requisitarPagSeguro();


class GerarPagamento {
    //put your code here
    private $cliente;
    private $inscricao;

     //pega os parâmetros do form
     public function getParametros(){
         $this->cliente= new Cliente();
         $this->cliente->setEndereco($_POST["endereco"]);
         $this->cliente->setBairro($_POST["bairro"]);
         $this->cliente->setCompl($_POST["compl"]);
         $this->cliente->setCidade($_POST["cidade"]);
          $this->cliente->setUf($_POST["uf"]);
          $cep=str_replace('-', '', $_POST["cep"]);
        $cep=str_replace('.', '',$cep);
         $this->cliente->setCep($cep);
        $this->cliente->setNum($_POST["num"]);
        $this->cliente->setTipo($_POST["tipo"]);
        $this->cliente->setNome($_POST["nome"]);
        $this->cliente->setRg($_POST["rg"]);
        $cpf=str_replace('-', '', $_POST["cpf"]);
        $cpf=str_replace('.', '',$cpf);
        $this->cliente->setCpf(cpf);
        $fone=str_replace('-', '', $_POST["fone"]);
        $this->cliente->setfone($fone);
        $this->cliente->setCod_fone($_POST["cod_fone"]);
        $this->cliente->setEmail($_POST["email"]);
        $this->cliente->setStatus(true);
        $this->cliente->setData_cad(date('Y-m-d'));

        $this->inscricao= new Inscricao();

        $this->inscricao->setCliente($this->cliente->getCpf());
        //$inscricao->setData_hora(date())


        if($this->cliente->getTipo()=="ESTUDANTE"){
              $valor = 30.00;
        }
        else{
              $valor=50.00;
         }
        $this->inscricao->setValor($valor);
        $this->inscricao->gereReferencia($this->cliente->getCpf(), $this->cliente->getData_cad());

     }


     public function requisitarPagSeguro(){
            $paymentRequest = new PaymentRequest();

		// Sets the currency
		$paymentRequest->setCurrency("BRL");

		// Add an item for this payment request
		$paymentRequest->addItem('0001', 'INSCRIÇÃO I CEAP - '.$this->cliente->getTipo(), 1, $this->inscricao->getValor());

		// Sets a reference code for this payment request, it is useful to identify this payment in future notifications.
		$paymentRequest->setReference($this->inscricao->getReferencia());

		// Sets shipping information for this payment request
		//$CODIGO_SEDEX = ShippingType::getCodeByType('SEDEX');
		$paymentRequest->setShippingType(3);
               // $endereco= new Shippin
		$paymentRequest->setShippingAddress($this->cliente->getCep(),  $this->cliente->getEndereco(),  $this->cliente->getNum(),  $this->cliente->getCompl(),  $this->cliente->getBairro(),  $this->cliente->getCidade(),  $this->cliente->getUf(), 'BRA');

		// Sets your customer information.
		$paymentRequest->setSender( $this->cliente->getNome(), $this->cliente->getEmail(), $this->cliente->getCod_fone(), $this->cliente->getFone());

		//$paymentRequest->setRedirectUrl("http://www.dimb.com.br");

		try {

			/*
			* #### Crendencials #####
			* Substitute the parameters below with your credentials (e-mail and token)
			* You can also get your credentails from a config file. See an example:
			* $credentials = PagSeguroConfig::getAccountCredentials();
			*/
			$credentials = new AccountCredentials("cesar.consultorjr@gmail.com", "D59489A7186C4CE5A0D3F168DC4F0041");

			// Register this payment request in PagSeguro, to obtain the payment URL for redirect your customer.
			$url = $paymentRequest->register($credentials);
                        //echo $url;
                        //header("Location: $url");
			//self::printPaymentUrl($url);
                        echo "<script language='javascript'>";
                        echo "document.location.href = '$url'";
                        echo "</script>";

		} catch (PagSeguroServiceException $e) {
			die($e->getMessage());
		}
     }
}
?>